package impl;

import java.io.DataInputStream;
import java.io.IOException;

public class ClientThread extends Thread {
	private Client client;
	private DataInputStream input;
	private static String RECEIVED_PROMPT = "Received> ";

	public ClientThread(Client client, DataInputStream input) {
		this.client = client;
		this.input = input;

		start();
	}

	// With the thread running the background this method will read all message
	// sent from other clients and display them in the console
	public void run() {
		try {
			while (client.isConnect()) {
				String message = input.readUTF();
				if (message != null && !message.equals("")) {
					System.out.println();
					System.out.println(RECEIVED_PROMPT + message);
				}
			}

			// if the user has selected to logout then return from the Thread to
			// stop it from checking for messages
			input.close();
			return;
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}
}
